﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>SendMessage - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The SendMessage function sends a message to a window or control and waits for acknowledgement." />
<meta name="ahk:equiv-v1" content="commands/PostMessage.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>SendMessage</h1>

<p>Sends a message to a window or control and waits for acknowledgement.</p>

<pre class="Syntax">Result := <span class="func">SendMessage</span>(Msg <span class="optional">, wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText, Timeout</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Msg</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The message number to send. See the <a href="../misc/SendMessageList.htm">message list</a> to determine the number.</p>
  </dd>

  <dt>wParam, lParam</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a>或<a href="../Concepts.htm#objects">对象</a></p>
    <p>The message parameters. If omitted, each parameter defaults to 0.</p>
    <p>Each parameter must be an <a href="../Concepts.htm#numbers">integer</a> or an object with a <a href="../objects/Buffer.htm#Ptr">Ptr</a> property, such as a <a href="../objects/Buffer.htm">Buffer</a>. For messages which require a pointer to a string, use a Buffer or the <a href="../Variables.htm#amp">address operator</a>. If the string contained by a variable is changed by passing the variable's address to SendMessage, the variable's length must be updated afterward by calling <a href="VarSetCapacity.htm#neg1">VarSetCapacity(MyVar, -1)</a>.</p>
    <p>If AutoHotkey or the target window is 32-bit, only the parameter's low 32 bits are used; that is, values are truncated if outside the range -2147483648 to 2147483647 (-0x80000000 to 0x7FFFFFFF) for signed values, or 0 to 4294967295 (0xFFFFFFFF) for unsigned values. If AutoHotkey and the target window are both 64-bit, any integer value <a href="../Concepts.htm#pure-numbers">supported by AutoHotkey</a> can be used.</p>
  </dd>

  <dt>Control</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>If this parameter is omitted, the message will be sent directly to the target window rather than one of its controls. Otherwise, this parameter can be the control's ClassNN, text or HWND, or an object with a <code>Hwnd</code> property. For details, 请参阅 <a href="Control.htm#Parameter">The Control Parameter</a>.</p>
    <p>If this parameter specifies a HWND (as an integer or object), it is not required to be the HWND of a control (child window). That is, it can also be the HWND of a top-level window.</p>
  </dd>

  <dt>WinTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
  </dd>

  <dt>WinText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
  </dd>

  <dt>ExcludeTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>标题中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>ExcludeText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>文本中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>Timeout</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>The maximum number of milliseconds to wait for the target window to process the message. If omitted, it defaults to 5000 (milliseconds), which is also the default behaviour in older versions of AutoHotkey which did not support this parameter. If the message is not processed within this time, the function finishes and sets ErrorLevel to 1.</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>This function returns an empty string if there was a problem or the message timed out. Otherwise, it returns the result of the message, which might sometimes be a &quot;reply&quot; depending on the nature of the message and its target window.</p>
<p>The range of possible values depends on the target window and the version of AutoHotkey that is running.  When using a 32-bit version of AutoHotkey, or if the target window is 32-bit, the result is a 32-bit unsigned integer between 0 and 4294967295. When using the 64-bit version of AutoHotkey with a 64-bit window, the result is a 64-bit signed integer between -9223372036854775808 and 9223372036854775807.</p>
<p>If the result is intended to be a 32-bit signed integer (a value from -2147483648 to 2147483648), it can be truncated to 32-bit and converted to a signed value as follows:</p>
<pre>MsgReply := MsgReply &lt;&lt; 32 &gt;&gt; 32</pre>
<p>This conversion may be necessary even on AutoHotkey 64-bit, because results from 32-bit windows are zero-extended. 例如, a result of -1 from a 32-bit window is seen as 0xFFFFFFFF on any version of AutoHotkey, whereas a result of -1 from a 64-bit window is seen as 0xFFFFFFFF on AutoHotkey 32-bit and -1 on AutoHotkey 64-bit.</p>

<h2>ErrorLevel</h2>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 1 if there was a problem such as the target window or control not existing, or if SendMessage timed out. Otherwise, it is set to 0.</p>

<h2 id="Remarks">备注</h2>
<p>This function should be used with caution because sending a message to the wrong window (or sending an invalid message) might cause unexpected behavior or even crash the target application. This is because most applications are not designed to expect certain types of messages from external sources.</p>
<p>SendMessage waits for the target window to process the message, up until the timeout period expires. By contrast, <a href="PostMessage.htm">PostMessage</a> places the message in the message queue associated with the target window without waiting for acknowledgement or reply.</p>
<p>String parameters must be passed by <a href="../Variables.htm#amp">address</a>. 例如:</p>
<pre>Run "Notepad"
WinWait "Untitled - Notepad"
SendMessage 0xC, 0, <strong>&amp;&quot;New Notepad Title&quot;</strong>  <em>; 0XC is WM_SETTEXT</em></pre>
<p>To send a message to all windows in the system, including those that are hidden or disabled, specify <code>ahk_id 0xFFFF</code> for <em>WinTitle</em> (0xFFFF is HWND_BROADCAST). This technique should be used only for messages intended to be broadcast, such as the following example:</p>
<pre>SendMessage 0x1A,,,, "ahk_id 0xFFFF"  <em>; 0x1A is WM_SETTINGCHANGE</em></pre>
<p>To have a script receive a message, use <a href="OnMessage.htm">OnMessage</a>.</p>
<p>See the <a href="../misc/SendMessage.htm">Message Tutorial</a> for an introduction to using this function.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>

<h2 id="Related">相关</h2>
<p><a href="PostMessage.htm">PostMessage</a>, <a href="../misc/SendMessageList.htm">Message List</a>, <a href="../misc/SendMessage.htm">Message Tutorial</a>, <a href="OnMessage.htm">OnMessage</a>, <a href="../misc/Winamp.htm">Automating Winamp</a>, <a href="DllCall.htm">DllCall</a>, <a href="ControlSend.htm">ControlSend</a>, <a href="MenuSelect.htm">MenuSelect</a></p>

<h2 id="Examples">示例</h2>

<div class="ex" id="ExMonitorPower">
<p><a href="#ExMonitorPower">#1</a>: Turns off the monitor via hotkey. In the SendMessage line, replace the number 2 with -1 to turn the monitor on or replace it with 1 to activate the monitor's low-power mode.</p>
<pre>#o::  <em>; Win+O hotkey</em>
Sleep 1000  <em>; Give user a chance to release keys (in case their release would wake up the monitor again).
; Turn Monitor Off:</em>
SendMessage 0x112, 0xF170, 2,, "Program Manager"  <em>; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.</em>
return</pre>
</div>

<div class="ex" id="ExScreenSave">
<p><a href="#ExScreenSave">#2</a>: Start the user's chosen screen saver:</p>
<pre>SendMessage 0x112, 0xF140, 0,, "Program Manager"  <em>; 0x112 is WM_SYSCOMMAND, and 0xF140 is SC_SCREENSAVE.</em></pre>
</div>

<div class="ex" id="ExVScrollUp">
<p><a href="#ExVScrollUp">#3</a>:  Scroll up by one line (for a control that has a vertical scroll bar):</p>
<pre>SendMessage 0x115, 0, 0, ControlGetFocus("A")</pre>
</div>

<div class="ex" id="ExVScrollDown">
<p><a href="#ExVScrollDown">#4</a>: Scroll down by one line:</p>
<pre>SendMessage 0x115, 1, 0, ControlGetFocus("A")</pre>
</div>

<div class="ex" id="ExWinamp">
<p><a href="#ExWinamp">#5</a>: This example asks Winamp which track number is currently active. See <a href="../misc/Winamp.htm">Automating Winamp</a> for more information.</p>
<pre>SetTitleMatchMode 2
track := SendMessage(1024, 0, 120,, "- Winamp")
if !ErrorLevel
{
    track++  <em>; Winamp's count starts at 0, so adjust by 1.</em>
    MsgBox "Track #" track " is active or playing."
}</pre>
</div>

<div class="ex" id="ExPID">
<p><a href="#ExPID">#6</a>: To find the process ID of an AHK script (an alternative to <a href="WinGetPID.htm">WinGetPID</a>):</p>
<pre class="NoIndent">SetTitleMatchMode 2
DetectHiddenWindows true
pid := SendMessage(0x44, 0x405, 0, , "SomeOtherScript.ahk - AutoHotkey v")
MsgBox pid " is the process id."</pre>
</div>

</body>
</html>